#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N=5e5+10;
int a[N],vis[N],q[N],T;
int n,m,pos,x;
int lord(int a,int b){
	int dlt=0;
	if(a<=b) return b-a;
	else{
		while(a/2>b){
			a/=2;
			dlt++;
		}
		int num=(a-b);
		num=min(num,b-a/2+1);
		return dlt+num;
	}
}
void solve(){
	int x,pos=0;
	ll res=0x3f3f3f3f;
	cin>>n>>m;
	vector<int> num;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		x=a[i];
		while(x){
			num.push_back(x);
			x/=2;
		};
	}
	
	sort(num.begin(),num.end());
	num.erase(unique(num.begin(),num.end()),num.end());
//	for(auto dt:num) cout<<dt<<' '<<endl;
	for(int i=0;i<num.size();i++){
		int pos1=0;
		ll dlt=0;
		for(int j=1;j<=n;j++) q[++pos1]=lord(a[j],num[i]);
		sort(q+1,q+1+pos1);
		for(int i=1;i<=n-m;i++) dlt+=q[i];
		res=min(res,dlt);
	}
	cout<<res<<endl;
}
int main(){
	scanf("%d",&T);
//	T=1;
//	cout<<lord(6,1);
	while(T--) solve();
	return 0;
}
